package sgi.client;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.TextField;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTable;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;

import sgi.message.MessageRetrieveTariff.TariffRow;

import java.awt.Color;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;

public class SGITariffUI extends SGIClientFrame implements KeyListener{

	private JPanel contentPane;
	private JTextField tfImageNew;
	private JTextField tfTimeLocSubNew;
	private JTextField tfImagePackSubNew;
	private JTextField tfDataLayerSubNew;
	private JTextField tfImageCur;
	private JTextField tfTimeLocSubCur;
	private JTextField tfImagePackSubCur;
	private JTextField tfDataLayerSubCur;
	private JButton btnSendToCeo;
	private JLabel lblApprovedordecline;
	private JButton btnUpdate;
	private SGITariffController sgiTariffController;
	
	


	public void setSgiTariffController(SGITariffController sgiTariffController) {
		this.sgiTariffController = sgiTariffController;
	}


	public SGITariffUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 739, 260);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setTitle("SGI Tariff");
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel_22 = new JPanel();
		panel_22.setBounds(0, 11, 705, 192);
		contentPane.add(panel_22);
		panel_22.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_23 = new JPanel();
		panel_22.add(panel_23, BorderLayout.NORTH);
		panel_23.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel_22.add(panel, BorderLayout.WEST);
		panel.setLayout(new BorderLayout(0, 0));
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SGITariffController.terminate();
				SGIUserAccountMenuController.create(null);
			}
		});
		panel.add(btnCancel, BorderLayout.SOUTH);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Current Tariff", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new GridLayout(0, 2, 0, 0));
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2);
		panel_2.setLayout(new GridLayout(4, 0, 0, 0));
		
		JPanel panel_3 = new JPanel();
		panel_2.add(panel_3);
		
		JLabel lblImage = new JLabel("Image");
		panel_3.add(lblImage);
		

		
		JPanel panel_4 = new JPanel();
		panel_2.add(panel_4);
		
		JLabel lblTimeLocationSubscription = new JLabel("Time Location Subscription");
		panel_4.add(lblTimeLocationSubscription);
		
		JPanel panel_5 = new JPanel();
		panel_2.add(panel_5);
		
		JLabel lblImagePackSubscription = new JLabel("Image Pack Subscription");
		panel_5.add(lblImagePackSubscription);
		
		JPanel panel_6 = new JPanel();
		panel_2.add(panel_6);
		
		JLabel lblDataLayerSubscription = new JLabel("Data Layer Subscription");
		panel_6.add(lblDataLayerSubscription);
		
		JPanel panel_7 = new JPanel();
		panel_1.add(panel_7);
		panel_7.setLayout(new GridLayout(4, 0, 0, 0));
		
		JPanel panel_8 = new JPanel();
		panel_7.add(panel_8);
		
		tfImageCur = new JTextField();
		tfImageCur.setEditable(false);
		tfImageCur.setColumns(10);
		panel_8.add(tfImageCur);
		

		
		JPanel panel_9 = new JPanel();
		panel_7.add(panel_9);
		
		tfTimeLocSubCur = new JTextField();
		tfTimeLocSubCur.setEditable(false);
		tfTimeLocSubCur.setColumns(10);
		panel_9.add(tfTimeLocSubCur);
		

		
		JPanel panel_10 = new JPanel();
		panel_7.add(panel_10);
		
		tfImagePackSubCur = new JTextField();
		tfImagePackSubCur.setEditable(false);
		tfImagePackSubCur.setColumns(10);
		panel_10.add(tfImagePackSubCur);

		JPanel panel_24 = new JPanel();
		panel_7.add(panel_24);
		
		tfDataLayerSubCur = new JTextField();
		tfDataLayerSubCur.setEditable(false);
		tfDataLayerSubCur.setColumns(10);
		panel_24.add(tfDataLayerSubCur);
		
		JPanel panel_26 = new JPanel();
		panel_22.add(panel_26, BorderLayout.CENTER);
		panel_26.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_27 = new JPanel();
		panel_26.add(panel_27, BorderLayout.NORTH);
		
		lblApprovedordecline = new JLabel("Approved");
		lblApprovedordecline.setForeground(Color.BLACK);
		panel_27.add(lblApprovedordecline);
		
		JPanel panel_28 = new JPanel();
		panel_28.setSize(new Dimension(150, 0));
		FlowLayout flowLayout = (FlowLayout) panel_28.getLayout();
		flowLayout.setVgap(55);
		panel_26.add(panel_28, BorderLayout.CENTER);
		

		
		btnUpdate = new JButton("Update");
		panel_28.add(btnUpdate);
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tfImageCur.setText(tfImageNew.getText());
				tfTimeLocSubCur.setText(tfTimeLocSubNew.getText());
				tfImagePackSubCur.setText(tfImagePackSubNew.getText());
				tfDataLayerSubCur.setText(tfDataLayerSubNew.getText());
				
				tfImageNew.setText("");
				tfTimeLocSubNew.setText("");
				tfImagePackSubNew.setText("");
				tfDataLayerSubNew.setText("");				
				
				setUiAsApproved(false);
				
				sgiTariffController.requestUpdate();
			}
		});
		btnUpdate.setEnabled(false);
		
		JPanel panel_11 = new JPanel();
		panel_22.add(panel_11, BorderLayout.EAST);
		panel_11.setLayout(new BorderLayout(0, 0));
		
		btnSendToCeo = new JButton("Send To CEO");
		btnSendToCeo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try{
					float image = Float.parseFloat(tfImageNew.getText());
					float timeLocation = Float.parseFloat(tfTimeLocSubNew.getText());
					float imagePack = Float.parseFloat(tfImagePackSubNew.getText());
					float dataLayer = Float.parseFloat(tfDataLayerSubNew.getText());
					
					sgiTariffController.changeTariff(image, timeLocation, imagePack, dataLayer);
					setUiAsPending();					
				}
				catch(NumberFormatException nfe){
					displayNotification("you must insert float number!");
				}
			}
		});
		btnSendToCeo.setEnabled(false);
		panel_11.add(btnSendToCeo, BorderLayout.SOUTH);
		
		JPanel panel_12 = new JPanel();
		panel_12.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "New Tariff", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_11.add(panel_12, BorderLayout.CENTER);
		panel_12.setLayout(new GridLayout(0, 2, 0, 0));
		
		JPanel panel_13 = new JPanel();
		panel_12.add(panel_13);
		panel_13.setLayout(new GridLayout(4, 0, 0, 0));
		
		JPanel panel_14 = new JPanel();
		panel_13.add(panel_14);
		
		JLabel lblImageNew = new JLabel("Image");
		panel_14.add(lblImageNew);
		
		JPanel panel_15 = new JPanel();
		panel_13.add(panel_15);
		
		JLabel lblTimeLocationSubscriptionNew = new JLabel("Time Location Subscription");
		panel_15.add(lblTimeLocationSubscriptionNew);
		
		JPanel panel_16 = new JPanel();
		panel_13.add(panel_16);
		
		JLabel lblImagePackSubscriptionNew = new JLabel("Image Pack Subscription");
		panel_16.add(lblImagePackSubscriptionNew);
		
		JPanel panel_17 = new JPanel();
		panel_13.add(panel_17);
		
		JLabel lblDataLayerSubscriptionNew = new JLabel("Data Layer Subscription");
		panel_17.add(lblDataLayerSubscriptionNew);
		
		JPanel panel_18 = new JPanel();
		panel_12.add(panel_18);
		panel_18.setLayout(new GridLayout(4, 0, 0, 0));
		
		JPanel panel_19 = new JPanel();
		panel_18.add(panel_19);
		
		tfImageNew = new JTextField();
		tfImageNew.setColumns(10);
		panel_19.add(tfImageNew);
		
		JPanel panel_20 = new JPanel();
		panel_18.add(panel_20);
		
		tfTimeLocSubNew = new JTextField();
		tfTimeLocSubNew.setColumns(10);
		panel_20.add(tfTimeLocSubNew);
		
		JPanel panel_21 = new JPanel();
		panel_18.add(panel_21);
		
		tfImagePackSubNew = new JTextField();
		tfImagePackSubNew.setColumns(10);
		panel_21.add(tfImagePackSubNew);
		
		JPanel panel_25 = new JPanel();
		panel_18.add(panel_25);
		
		tfDataLayerSubNew = new JTextField();
		tfDataLayerSubNew.addKeyListener(this);
		tfDataLayerSubNew.setColumns(10);
		panel_25.add(tfDataLayerSubNew);
	}


	@Override
	public void keyPressed(KeyEvent arg0) {
		//not relevant
	}


	@Override
	public void keyReleased(KeyEvent arg0) {
		String imageNew = this.tfImageNew.getText();
		String timeLocNew = this.tfTimeLocSubNew.getText();
		String imageBackNew = this.tfImagePackSubNew.getText();
		String dataKayerNew = this.tfDataLayerSubNew.getText();
		
		boolean enableOrDisable = ifAllNotEmptyOrNull(imageNew,timeLocNew,imageBackNew,dataKayerNew);
		
		btnSendToCeo.setEnabled(enableOrDisable);
	}


	@Override
	public void keyTyped(KeyEvent keyEvent) {
		//not relevant	
	}
	
	private boolean ifAllNotEmptyOrNull(String... stringArgs)
	{
		for (String string : stringArgs) {
			if(string == null || string.isEmpty())
				return false;
				
		}
		
		return true;

	}
	
	public void displayNotification(String str)
	{
		JOptionPane.showMessageDialog(this,str);
	}


	public void loadCurrentTariff(TariffRow currentTariff) {
		tfImageCur.setText(String.valueOf(currentTariff.getImage()));
		tfTimeLocSubCur.setText(String.valueOf(currentTariff.getTimeLocation()));
		tfImagePackSubCur.setText(String.valueOf(currentTariff.getImagePack()));
		tfDataLayerSubCur.setText(String.valueOf(currentTariff.getDataLayer()));	
		
		setUiAsApproved(false);
	}


	public void loadPendingTariff(TariffRow pendingTariff) {
		tfImageNew.setText(String.valueOf(pendingTariff.getImage()));
		tfTimeLocSubNew.setText(String.valueOf(pendingTariff.getTimeLocation()));
		tfImagePackSubNew.setText(String.valueOf(pendingTariff.getImagePack()));
		tfDataLayerSubNew.setText(String.valueOf(pendingTariff.getDataLayer()));
		
		switch (pendingTariff.getStatus()) {
		case 1:
			setUiAsApproved(true);
			break;
		case 2:
			setUiAsPending();
			break;	
		case 3:
			setUiAsDeclined();
			break;
		}
	}
	
	private void setUiAsApproved(boolean useUpdate){
		lblApprovedordecline.setText("Approved");
		lblApprovedordecline.setForeground(Color.GREEN);
		
		if(useUpdate){
			btnUpdate.setEnabled(true);
			
			tfImageNew.setEditable(false);
			tfTimeLocSubNew.setEditable(false);
			tfImagePackSubNew.setEditable(false);
			tfDataLayerSubNew.setEditable(false);
		}
		else{
			tfImageNew.setEditable(true);
			tfTimeLocSubNew.setEditable(true);
			tfImagePackSubNew.setEditable(true);
			tfDataLayerSubNew.setEditable(true);
			
			btnUpdate.setEnabled(false);
		}
	}
	
	private void setUiAsDeclined(){
		lblApprovedordecline.setText("Declined");
		lblApprovedordecline.setForeground(Color.RED);
		
		tfImageNew.setEditable(true);
		tfTimeLocSubNew.setEditable(true);
		tfImagePackSubNew.setEditable(true);
		tfDataLayerSubNew.setEditable(true);
		
		tfImageNew.setText("");
		tfTimeLocSubNew.setText("");
		tfImagePackSubNew.setText("");
		tfDataLayerSubNew.setText("");
	}

	private void setUiAsPending() {
		lblApprovedordecline.setText("Pending");
		lblApprovedordecline.setForeground(Color.ORANGE);
		
		tfImageNew.setEditable(false);
		tfTimeLocSubNew.setEditable(false);
		tfImagePackSubNew.setEditable(false);
		tfDataLayerSubNew.setEditable(false);
		btnSendToCeo.setEnabled(false);
	}
	


	public void updateOnlineApprove(boolean approve) {
		if(approve){
			setUiAsApproved(approve);
		}
		else{
			setUiAsDeclined();
		}
	}
}
